/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.myfaces.extensions.cdi.forge.api;

import org.apache.myfaces.extensions.cdi.forge.api.support.ViewConfigException;
import org.apache.myfaces.extensions.cdi.forge.api.support.ViewConfigMetadata;
import org.apache.myfaces.extensions.cdi.forge.api.support.ViewConfigUtil;
import org.jboss.forge.shell.Shell;
import org.jboss.forge.shell.completer.CommandCompleter;
import org.jboss.forge.shell.completer.CommandCompleterState;

import javax.inject.Inject;

/**
 * @author Cosmin Martinconi, 19.08.2011
 */
public class ViewConfigCompleter implements CommandCompleter {

    private final Shell shell;

    @Inject
    public ViewConfigCompleter(final Shell shell) {
        this.shell = shell;
    }

    @Override
    public void complete(CommandCompleterState state) {

        if (state.getTokens().isEmpty()) {
            for (ViewConfigUtil vcUtil : ViewConfigUtil.getAllViewConfigs(shell)) {
                state.getCandidates().add(vcUtil.getViewConfigMetadata().getName() + " ");
            }
        } else {
            ViewConfigUtil vcUtil;
            ViewConfigMetadata vcMetadata = null;

            String userInput = state.getTokens().peek();
            if (userInput.contains(".")) {
                String viewConfigName = userInput.substring(0, userInput.indexOf("."));
                try {
                    vcUtil = new ViewConfigUtil(shell, viewConfigName);
                } catch (ViewConfigException e){
                    return;
                }

                String parentMetadataName;
                String searchMetadataExpr = "";
                try {
                    vcMetadata = vcUtil.getViewConfigMetadata(userInput);
                    if (!userInput.endsWith(".")) {
                        if (vcMetadata.getMetadataChildList().isEmpty()) {
                            state.getCandidates().add(" ");
                        } else {
                            state.getCandidates().add(".");
                        }
                    } else {
                        for (ViewConfigMetadata metadata : vcMetadata.getMetadataChildList()) {
                            if (metadata.getName().startsWith(searchMetadataExpr)) {
                                state.getCandidates().add(metadata.getName());
                            }
                        }
                    }
                } catch (ViewConfigException e){
                    parentMetadataName = userInput.substring(0, userInput.lastIndexOf("."));
                    searchMetadataExpr = userInput.substring(userInput.lastIndexOf(".") + 1);
                    try{
                        vcMetadata = vcUtil.getViewConfigMetadata(parentMetadataName);
                    } catch (ViewConfigException e2) {
                        return;
                    }

                    for (ViewConfigMetadata metadata : vcMetadata.getMetadataChildList()) {
                        if (metadata.getName().startsWith(searchMetadataExpr)) {
                            state.getCandidates().add(metadata.getName());
                            state.setIndex(state.getOriginalIndex() - searchMetadataExpr.length());
                        }
                    }
                }
            } else {
                try {
                    vcUtil = new ViewConfigUtil(shell, userInput);
                        if (vcUtil.getViewConfigMetadata().getMetadataChildList().isEmpty()) {
                            state.getCandidates().add(" ");
                        } else {
                            state.getCandidates().add(".");
                        }
                } catch (ViewConfigException e){
                    for (ViewConfigUtil util : ViewConfigUtil.getAllViewConfigs(shell)) {
                        if (util.getViewConfigMetadata().getName().startsWith(userInput)) {
                            state.getCandidates().add(util.getViewConfigMetadata().getName());
                            state.setIndex(state.getOriginalIndex() - (userInput == null ? 0 : userInput.length()));
                        }
                    }
                }
            }
        }
    }
}
